#ifndef HTTP_LIB_PUSH_CONSOLE_H
#define HTTP_LIB_PUSH_CONSOLE_H

#include "http_lib_push.h"

class Http_lib_push_console
{
public:
    Http_lib_push_console() {};
    ~Http_lib_push_console()
    {
        std::lock_guard<std::mutex> lock(http_push_mtx_);
        for (auto &it : http_push_map_)
        {
            it.second->stop();
        }
    };

    int get_channel_num();
    void add_channel(std::string url, std::string path, bool send_img_base64);
    void push_img(int channel_idx, std::shared_ptr<FrameInfoDetectYolov8Seg> img_ptr);

private:
    std::unordered_map<int, std::shared_ptr<HttpLibPush>> http_push_map_;
    std::mutex http_push_mtx_;
    int que_size();
};

int Http_lib_push_console::get_channel_num()
{
    std::lock_guard<std::mutex> lock(http_push_mtx_);
    return http_push_map_.size();
}

void Http_lib_push_console::add_channel(std::string url, std::string path, bool send_img_base64)
{

    int channel_idx = get_channel_num();
    http_push_map_.insert(std::make_pair(channel_idx, std::make_shared<HttpLibPush>(url, path, 5,15,15)));
    http_push_map_[channel_idx]->start();
    std::cout << "http channel add success, url: " << url << ", path: " << path << std::endl;
}

void Http_lib_push_console::push_img(int channel_idx, std::shared_ptr<FrameInfoDetectYolov8Seg> img_ptr)
{
    if(channel_idx < 0 || channel_idx >= get_channel_num())
    {
        std::cout << "http chnannel idx error, channel_idx: " << channel_idx << std::endl;
        return;
    }
    http_push_map_[channel_idx]->push_img(channel_idx, img_ptr);
}

#endif // HTTP_LIB_PUSH_CONSOLE_H